﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace Library
{
	/// <summary>
	///  李天 2008-11-27
	/// </summary>
	public class DateHelper
	{
		/// <summary>
		///  得到某天是一年中的第几周
		/// </summary>
		/// <param name="date"></param>
		/// <returns></returns>
		public static int GetWeekOfYear(DateTime date)
		{
			CultureInfo myCI = new CultureInfo("zh-TW");

			Calendar myCal = myCI.Calendar;

			CalendarWeekRule myCWR = myCI.DateTimeFormat.CalendarWeekRule;

			DayOfWeek myFirstDOW = DayOfWeek.Sunday;

			int wd = myCal.GetWeekOfYear(date, myCWR, myFirstDOW);
			
			return wd;
		}

		/// <summary>
		///  返回最近几天、最近几周、最近几月等的开始时间和结束时间段
		/// </summary>
		/// <param name="date"></param>
		/// <param name="type">type为0表示天，为1表示周,为2表示月，为3表示年</param>
		public static void GetDateStartAndEnd(DateTime date, DateType dateType, ref DateTime startTime, ref DateTime endTime, int diff)
		{
			int day = date.Day;
			int startHour = 0;
			int startMinute = 0;
			int startSecond = 0;
			int endHour = 23;
			int endMinute = 59;
			int endSecond = 59;
			int diffDays = 0;

			startTime = new DateTime(date.Year, date.Month, date.Day, startHour, startMinute, startSecond);
			endTime = new DateTime(date.Year, date.Month, date.Day, endHour, endMinute, endSecond);

			switch (dateType)
			{
				case DateType.Day:

					startTime = startTime.AddDays(-diff);
					endTime = endTime.AddDays(-diff);

					break;
				case DateType.Week:

					//if (diff == 0) { diff = 1; }

					diffDays = 7 * diff;
					//diffDays += startTime.DayOfWeek.GetHashCode() - 1 % 7;

					switch (startTime.DayOfWeek)
					{
						case DayOfWeek.Monday:
							//diffDays = 7;
							break;
						case DayOfWeek.Tuesday:
							diffDays = diffDays + 1;
							break;
						case DayOfWeek.Wednesday:
							diffDays = diffDays + 2;
							break;
						case DayOfWeek.Thursday:
							diffDays = diffDays + 3;
							break;
						case DayOfWeek.Friday:
							diffDays = diffDays + 4;
							break;
						case DayOfWeek.Saturday:
							diffDays = diffDays + 5;
							break;
						case DayOfWeek.Sunday:
							diffDays = diffDays + 6;
							break;
					}

					startTime = startTime.AddDays(-diffDays);
					int endDays = (diffDays - 7) + 1;
					endTime = endTime.AddDays(-endDays);
					break;

				case DateType.Month:

					System.Globalization.CultureInfo myCI = new System.Globalization.CultureInfo("zh-TW");

					System.Globalization.Calendar myCal = myCI.Calendar;

					System.Globalization.CalendarWeekRule myCWR = myCI.DateTimeFormat.CalendarWeekRule;
					
					DayOfWeek myFirstDOW = DayOfWeek.Sunday;
					startTime = startTime.AddMonths(-diff);
					startTime = startTime.AddDays(-(startTime.Day - 1));

					endTime = endTime.AddMonths(-diff);
					int daysInMonth = myCal.GetDaysInMonth(endTime.Year, endTime.Month);
					endTime = endTime.AddDays(daysInMonth - endTime.Day);

					break;
			}
		}

		/// <summary>
		///  转换为YYYY-MM-DD格式的日期字符串
		/// </summary>
		/// <param name="date"></param>
		/// <returns></returns>
		public static string ConvertToYYYMMDD(DateTime date)
		{
			return date.ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// 转换为yyyy-mm-dd 00:00:00格式的日期
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static DateTime ConvertToYYMMDDTime(DateTime date)
        {
            string dt = date.ToShortDateString();
            return DateTime.Parse(dt);
        }

		/// <summary>
		///  验证时间是否有效
		/// </summary>
		/// <param name="strTime"></param>
		/// <returns></returns>
		public static bool IsValidateTime(string strTime)
		{

			try
			{
				DateTime date = DateTime.Parse(strTime);
				return true;
			}
			catch
			{
				return false;
			}

		}

		private static bool IsOverRangeOfDatabaseDate(DateTime dt)
		{
			return dt < Library.DefaultValue.MinDateTime || dt > Library.DefaultValue.MaxDateTime;
		}

		/// <summary>
		/// 日期类型数字转换
		/// 1. 转换成数据库允许的日期范围
		/// 2. 转换成开始时间小于结束时间
		/// 3. 开始时间转换成所选日期最小值
		/// 4. 结束时间转换成所选日期最大值
		/// </summary>
		/// <param name="datestart">开始日期</param>
		/// <param name="dateend">结束日期</param>
		public static void DatabaseDateValidate(ref DateTime datestart,ref DateTime dateend)
		{
			if (IsOverRangeOfDatabaseDate(datestart))
				datestart = Library.DefaultValue.MinDateTime;
			if (IsOverRangeOfDatabaseDate(dateend))
				dateend = Library.DefaultValue.MaxDateTime;
			if (datestart > dateend)
				dateend = datestart;
			datestart = new DateTime(datestart.Year, datestart.Month, datestart.Day, 0, 0, 0, 0);
			dateend = new DateTime(dateend.Year, dateend.Month, dateend.Day, 23, 59, 59, 997);
		}
	}

	public enum DateType
	{
		Day =0,
		Week =1,
		Month =2,
		//Year = 3
	}

}
